Golang - cross-compiling
Published Date: 2019-06-16 15:57:39Z
About the Golang cross-compiling,say it simply that Windows build codes can run not only in Windows,but in Linux (Mac not test).
I work between Win and Linux,may I have build the code in Windows easy,but it in another pc maybe difficult.At least,you have to redownload the libs you use.And I have try the Cross-compiling.
In command or Terminal(installed golang >=go1.11.2),you can run “go version” to get your system’s info,save it and use it later.
Set Environment var
Windows:
in commmand : set var = value
Linux :
in terminal :export var = value
Keyin the command “go env” and find out the keys “GOOS”(windows,linux) and “GOARCH”(amd64 , arm).
If error with gcc,add up the gcc key ;
if error with CGO,set CGO_ENABLED=0 ;
if error with ‘cannot find package golang.org/x/sys/unix
,git clone https://github.com/golang/sys.git
to gopath’s golang.org/x/sys
.
At last go build the code.
Example:
Windows(Windows's info:windows,amd64) build Binary executable file in Linux(Linux's info:linux ,arm):
keyin command
set GOOS="linux"
set GOARCH="arm"
set CGO_ENABLED=0
set GOEXE=
go build helloworld.go
Same infos,Linux build Binary executable file in Windows:
set GOOS="windows"
set GOARCH="amd64"
set GOEXE=.exe
go build helloworld.go
Get helloworld.exe and run in windows;get helloword and run in Linux.
关于Golang交叉编译,简单地说,Windows构建代码不仅可以在Windows中运行,而且可以在Linux中运行(Mac不测试)。
我在Win和Linux之间工作,我可以在Windows中轻松构建代码,但在另一台PC中可能很难。至少,你必须重新下载你使用的库。我尝试了交叉编译。
在命令或Termial(安装golang> = go1.11.2)中,您可以运行“go version”来获取系统信息,保存并稍后使用。
设置环境变量
Windows:
命令行 : set var = value
Linux :
终端 : export var = value
键入命令“go env”并找出键“GOOS”(windows,linux)和“GOARCH”(amd64,arm)。
如果gcc出错,请加上gcc键;
如果CGO出错,则设置CGO_ENABLED = 0;
如果错误'找不到包golang.org/x/sys/unix
,就git clone https://github.com/golang/sys.git
到gopath的golang.org/x/sys
。
最后去构建代码。
例:
Windows(Windows的信息:windows,amd64)在Linux中构建二进制可执行文件(Linux的信息:linux,arm):
键入命令
set GOOS ="linux"
set GOARCH ="arm"
set CGO_ENABLED = 0
set GOEXE =
go build helloworld.go
相同的信息,Linux构建Windows中的二进制可执行文件:
export GOOS ="windows"
export GOARCH ="amd64"
export GOEXE = .exe
go build helloworld.go
获取helloworld.exe并在Windows中运行;获取helloword并在Linux中运行